package groovy.syntax

/**
 * @author houjinxin
 * groovy标示符 
 * 1、以字母, 美元符号, 下划线开头，不以数字开头
 * 2、字母包括大小写字母[a-z|A-Z]
 * 3、所有的关键字在"."以后，则可以作为对象的属性
 * 4、
 */

//合法的变量定义
def name
def item3
def with_underscore
def $dollarStart

//不合法的定义，逐行放开注释可以看到错误提示
//def 3tier
//def a+b
//def a#b

//关键字作为MAP对象属性
def foo = [:]
foo.as 
foo.assert
foo.break
foo.case
foo.catch

//三种取属性的方式等价
def person = [name:'houjinxin']
assert person.name == person.'name'
assert person.name == person."name"
assert person.'name' == person."name"

//groovy中允许一些java不允许的方式定义属性，如属性中包含空格、 -等 在java中则会报错
def map = [:]
map."an identifier with a space and double quotes" = "ALLOWED"
map.'with-dash-signs-and-single-quotes' = "ALLOWED"

assert map."an identifier with a space and double quotes" == "ALLOWED"
assert map.'with-dash-signs-and-single-quotes' == "ALLOWED"

//各种类型的字符串
map.'single quote'
map."double quote"
map.'''triple single quote'''
map."""triple double quote"""
map./slashy string/
map.$/dollar slashy string/$

def a = /aaa/ instanceof String
def b = $/dollar slashy string/$ instanceof String
assert a == b

//普通字符串和groovy的GString GString 是指在嵌入到普通字符串中的变量，最终运行时用指定值替换嵌入变量的字符串
def firstname = "Homer"
map."Simson-${firstname}" = "Homer Simson"

assert map.'Simson-Homer' == "Homer Simson"

